﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Smatime.Web.DAO;
using Smatime.Web.Models;

namespace Smatime.Web.Controllers
{
    public class CompanyStoreController : ApiController
    {
        // GET api/companystore
        public IEnumerable<CompanyStore> Get()
        {
            return new CompanyStoreRepository().GetList();
        }

        [Route("api/companystore/companyid/{companyId}")]
        public IEnumerable<CompanyStore> GetCompany(int companyId)
        {
            return new CompanyStoreRepository().GetStoreByCompanyId(companyId);
        }

        // GET api/companystore/5
        public CompanyStore Get(int id)
        {
            return new CompanyStoreRepository().GetById(id);
        }

        // Get by store name
        //[Route("api/companystore/companyId/{Id}/getstorename/{name}")]
        //public CompanyStore GetStoreName(long Id, string name)
        //{
        //    return new CompanyStoreRepository().GetByStoreName(Id, name);
        //    try
        //    {

        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    }
        //}
        [Route("api/companystore/storename")]
        public HttpResponseMessage PostStoreName([FromBody] CompanyStore value)
        {
            try
            {
                var repo = new CompanyStoreRepository();
                var id = value.CompanyId;
                var name = value.Name;
                var result = repo.GetByStoreName(id, name);
                return Request.CreateResponse(HttpStatusCode.NotAcceptable, result);
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.OK);
            }
        }

        //Get by store Id
        [Route("api/companystore/companyId/{companyId}/storeid/{id}")]
        public CompanyStore GetStoreId(long companyId, long id)
        {
            return new CompanyStoreRepository().GetByStoreId(companyId, id);
        }

        // POST api/companystore
        public void Post([FromBody] CompanyStore value)
        {
            var repos = new CompanyStoreRepository();
            repos.Insert(value);
        }

        // PUT api/companystore/5
        public void Put(int id, [FromBody] CompanyStore value)
        {
            var repos = new CompanyStoreRepository();
            value.Id = id;
            repos.Update(value);
        }

        // DELETE api/companystore/5
        public void Delete(int id)
        {
            var repos = new CompanyStoreRepository();
            repos.Delete(id);
        }
    }
}